﻿using System;
using System.Windows.Forms;

using HostsManager.Model;

namespace HostsManager.Controls
{
    public partial class PreviewControl : UserControl
    {
        private Hosts currentHosts = null;
        HtmlDocument document = null;
        HtmlElement content = null;

        public PreviewControl()
        {
            InitializeComponent();
            InitializeDocument();

            this.btnShowGlobal.Checked = Properties.Settings.Default.ShowGlobals;
            this.btnShowDisabled.Checked = Properties.Settings.Default.ShowDisabled;
        }

        private void InitializeDocument()
        {
            if (webBrowser1.Document == null || webBrowser1.Document.Body == null)
                webBrowser1.Navigate("about:blank");
            else
                webBrowser1.Document.OpenNew(true);

            this.webBrowser1.Document.Write(
                "<html>" + Environment.NewLine +
                "<head>" + Environment.NewLine +
                "  <style>" + strings.CSSStyle + "</style>" + Environment.NewLine +
                "</head>" + Environment.NewLine +
                "<body>" + Environment.NewLine +
                "  <div id=\"content\"></div>" + Environment.NewLine +
                "</body>" + Environment.NewLine +
                "</html>");

            this.document = webBrowser1.Document;
            this.content = webBrowser1.Document.GetElementById("content");
        }

        internal void ShowHosts(Hosts hosts)
        {
            if (hosts == null || content == null)
                return;

            this.currentHosts = hosts;

            // StringBuilder sb = new StringBuilder();

            // sb.Append("<div id=\"hosts\">");

            HtmlElement hostsElement = document.CreateElement("div");
            hostsElement.Id = "hosts";

            foreach (Section section in hosts.Sections.Values)
            {
                string CssClass = "section";
                if ((section == hosts.DefaultSection) && !btnShowGlobal.Checked)
                    CssClass = "hidden";
                else if (!section.Enabled)
                    CssClass = "disabledSection";

                // sb.AppendLine("<div id=\"" + section.Name + "\" class=\"" + CssClass + "\">");

                HtmlElement sectionElement = document.CreateElement("div");
                sectionElement.Id = section.Name;
                sectionElement.SetAttribute("className", CssClass);
                hostsElement.AppendChild(sectionElement);

                if (section != hosts.DefaultSection)
                {
                    // sb.AppendLine("<h1>" + section.Name + "</h1>");
                    HtmlElement sectionTitleElement = document.CreateElement("h1");
                    sectionTitleElement.InnerHtml = section.Name;
                    sectionElement.AppendChild(sectionTitleElement);
                }

                foreach (Configuration configuration in section.Configurations)
                {
                    CssClass = "configuration";
                    if (!section.Enabled || !configuration.Enabled)
                        if (!btnShowDisabled.Checked)
                            CssClass = "hidden";
                        else
                            CssClass = "disabledConfiguration";

                    // sb.AppendLine("<div id=\"" + section.Name + "." + configuration.Name + "\" class=\"" + CssClass + "\">");

                    HtmlElement configurationElement = document.CreateElement("div");
                    configurationElement.Id = section.Name + "." + configuration.Name;
                    configurationElement.SetAttribute("className", CssClass);
                    sectionElement.AppendChild(configurationElement);

                    if (configuration != hosts.DefaultConfiguration)
                    {
                        //sb.AppendLine("<h2>" + configuration.Name + "</h2>");
                        HtmlElement configurationTitleElement = document.CreateElement("h2");
                        configurationTitleElement.InnerHtml = configuration.Name;
                        configurationElement.AppendChild(configurationTitleElement);
                    }

                    // sb.AppendLine(configuration.Content);
                    HtmlElement configurationContentElement = document.CreateElement("div");
                    configurationContentElement.InnerHtml = configuration.Content.Replace(Environment.NewLine, "<br/>");
                    configurationElement.AppendChild(configurationContentElement);

                    //sb.AppendLine("</div>"); // configuration end
                }

                //sb.AppendLine("</div>"); // section end
            }

            //sb.AppendLine("</div>"); // hosts end

            content.InnerHtml = string.Empty;
            content.AppendChild(hostsElement);

            //content.OuterHtml = sb.ToString().Replace(Environment.NewLine, "<br/>");
            //content.AppendChild(doc.ChildNodes[0]);
        }

        private void btnHideGlobal_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.ShowGlobals = this.btnShowGlobal.Checked;
            Properties.Settings.Default.Save();

            // TODO: no need to reload... just dinamically change style
            ShowHosts(currentHosts);
        }

        private void btnHideDisabled_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.ShowDisabled = this.btnShowDisabled.Checked;
            Properties.Settings.Default.Save();

            // TODO: no need to reload... just dinamically change style
            ShowHosts(currentHosts);
        } 
    }
}
